package com.dunqi.gpm.cangxi.home.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.cangxi.develop.activity.ProjectTrendDetailActivity;
import com.dunqi.gpm.cangxi.home.adapter.ProjectWorkTrendAdapter;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.ProjectWorkTrend;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/22 0022.
 */

public class ProjectTrendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    ArrayList<ProjectWorkTrend> data = new ArrayList<>();
    private ProjectWorkTrendAdapter adapter;
    private long projectId;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryProjectWorkTrendList(projectId, page)
                .compose(RxSchedulers.<Page<ProjectWorkTrend>>io_main())
                .subscribe(new Consumer<Page<ProjectWorkTrend>>() {
                    @Override
                    public void accept(Page<ProjectWorkTrend> pager) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);

                        ArrayList<ProjectWorkTrend> list = pager.getList();

                        if (page == 1) {
                            data.clear();
                        }

                        data.addAll(list);

                        adapter.notifyDataSetChanged();

                        if (pager.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        recycler.addItemDecoration(new RecyclerItemDecoration());
        adapter = new ProjectWorkTrendAdapter(data);
        recycler.setAdapter(adapter);

        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnItemClickListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        adapter.setEmptyView(new EmptyView(context));
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        ProjectWorkTrend trend = data.get(position);
        ProjectTrendDetailActivity.start((Activity)context,trend.getProWorkTrendId());
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_project_trend;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        projectId = arguments.getLong(Constance.PROJECTID, -1);
    }

    public static ProjectTrendFragment newInstance(long projectId) {
        ProjectTrendFragment fragment = new ProjectTrendFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(Constance.PROJECTID, projectId);
        fragment.setArguments(bundle);
        return fragment;

    }

}

